<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="180px">
      <el-form-item label="电池组额定容量" prop="batteryGroupCapacity">
        <el-input
          v-model="queryParams.batteryGroupCapacity"
          placeholder="请输入电池组额定容量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电池组充电次数" prop="batteryGroupChargeTimes">
        <el-input
          v-model="queryParams.batteryGroupChargeTimes"
          placeholder="请输入电池组充电次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电池组生产日期" prop="batteryGroupManufactureDate">
        <el-input
          v-model="queryParams.batteryGroupManufactureDate"
          placeholder="请输入电池组生产日期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电池组序号" prop="batteryGroupSerial">
        <el-input
          v-model="queryParams.batteryGroupSerial"
          placeholder="请输入电池组序号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电池组额定电压" prop="batteryGroupVoltage">
        <el-input
          v-model="queryParams.batteryGroupVoltage"
          placeholder="请输入电池组额定电压"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电池生产厂家" prop="batteryManufacturer">
        <el-input
          v-model="queryParams.batteryManufacturer"
          placeholder="请输入电池生产厂家"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属用户" prop="consumerId">
        <el-input
          v-model="queryParams.consumerId"
          placeholder="请输入所属用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标称总能量" prop="countBatteryeenergy">
        <el-input
          v-model="queryParams.countBatteryeenergy"
          placeholder="请输入标称总能量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="免扫码授权" prop="directCharge">
        <el-input
          v-model="queryParams.directCharge"
          placeholder="请输入免扫码授权"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单体电池最高允许充电电压" prop="maxBatteryCellChargeVoltage">
        <el-input
          v-model="queryParams.maxBatteryCellChargeVoltage"
          placeholder="请输入单体电池最高允许充电电压"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最高允许充电电流" prop="maxChargeCurrents">
        <el-input
          v-model="queryParams.maxChargeCurrents"
          placeholder="请输入最高允许充电电流"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最高允许充电总电压" prop="maxCountChargeVoltage">
        <el-input
          v-model="queryParams.maxCountChargeVoltage"
          placeholder="请输入最高允许充电总电压"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最高允许温度" prop="maxTemperature">
        <el-input
          v-model="queryParams.maxTemperature"
          placeholder="请输入最高允许温度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆号" prop="vehicleNum">
        <el-input
          v-model="queryParams.vehicleNum"
          placeholder="请输入车辆号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:vehicle:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:vehicle:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:vehicle:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:vehicle:filter']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <!--<el-popover placement="right" title="列筛选" trigger="click" width="900" style="float: left; font-size: 12px;" @show="createData" @hide="deleteData">
          <el-checkbox-group v-model="checkedColumns" size="mini">
            <el-checkbox
              v-for="item in checkBoxGroup"
              :key="item"
              :label="item"
              :value="item"
            ></el-checkbox>
          </el-checkbox-group>
          <el-button slot="reference" type="primary" size="mini" plain>
            <i class="el-icon-arrow-down el-icon-menu" />列表项展示筛选</el-button>
        </el-popover>-->

      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList">
      </right-toolbar>
    </el-row>

    <el-table ref="myTable" v-loading="loading" :data="vehicleList" @selection-change="handleSelectionChange">
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <!--<div v-if="colDate.length > 0">

        <el-table-column v-for="(item,key) in colDate" :key="key" :label="item.label" align="center" :prop="item.prop" />
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="操作11" align="center" class-name="small-padding fixed-width"/>
      </div>
      <div v-else>-->
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="车辆识别码" align="center" prop="vin" />
        <el-table-column label="电池组额定容量" align="center" prop="batteryGroupCapacity" />
        <el-table-column label="电池组充电次数" align="center" prop="batteryGroupChargeTimes" />
        <el-table-column label="电池组生产日期" align="center" prop="batteryGroupManufactureDate" />
        <el-table-column label="电池组序号" align="center" prop="batteryGroupSerial" />
        <el-table-column label="电池组额定电压" align="center" prop="batteryGroupVoltage" />
        <el-table-column label="电池生产厂家" align="center" prop="batteryManufacturer" />
        <el-table-column label="电池类型" align="center" prop="batteryType" />
        <el-table-column label="所属用户" align="center" prop="consumerId" />
        <el-table-column label="标称总能量" align="center" prop="countBatteryeenergy" />
        <el-table-column label="免扫码授权" align="center" prop="directCharge" />
        <el-table-column label="单体电池最高允许充电电压" align="center" prop="maxBatteryCellChargeVoltage" />
        <el-table-column label="最高允许充电电流" align="center" prop="maxChargeCurrents" />
        <el-table-column label="最高允许充电总电压" align="center" prop="maxCountChargeVoltage" />
        <el-table-column label="最高允许温度" align="center" prop="maxTemperature" />
        <el-table-column label="车辆号" align="center" prop="vehicleNum" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width"/>
     <!-- </div>-->
      <el-table-column>
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:vehicle:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:vehicle:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="电池组额定容量" prop="batteryGroupCapacity">
          <el-input v-model="form.batteryGroupCapacity" placeholder="请输入电池组额定容量" />
        </el-form-item>
        <el-form-item label="电池组充电次数" prop="batteryGroupChargeTimes">
          <el-input v-model="form.batteryGroupChargeTimes" placeholder="请输入电池组充电次数" />
        </el-form-item>
        <el-form-item label="电池组生产日期" prop="batteryGroupManufactureDate">
          <el-input v-model="form.batteryGroupManufactureDate" placeholder="请输入电池组生产日期" />
        </el-form-item>
        <el-form-item label="电池组序号" prop="batteryGroupSerial">
          <el-input v-model="form.batteryGroupSerial" placeholder="请输入电池组序号" />
        </el-form-item>
        <el-form-item label="电池组额定电压" prop="batteryGroupVoltage">
          <el-input v-model="form.batteryGroupVoltage" placeholder="请输入电池组额定电压" />
        </el-form-item>
        <el-form-item label="电池生产厂家" prop="batteryManufacturer">
          <el-input v-model="form.batteryManufacturer" placeholder="请输入电池生产厂家" />
        </el-form-item>
        <el-form-item label="所属用户" prop="consumerId">
          <el-input v-model="form.consumerId" placeholder="请输入所属用户" />
        </el-form-item>
        <el-form-item label="标称总能量" prop="countBatteryeenergy">
          <el-input v-model="form.countBatteryeenergy" placeholder="请输入标称总能量" />
        </el-form-item>
        <el-form-item label="免扫码授权" prop="directCharge">
          <el-input v-model="form.directCharge" placeholder="请输入免扫码授权" />
        </el-form-item>
        <el-form-item label="单体电池最高允许充电电压" prop="maxBatteryCellChargeVoltage">
          <el-input v-model="form.maxBatteryCellChargeVoltage" placeholder="请输入单体电池最高允许充电电压" />
        </el-form-item>
        <el-form-item label="最高允许充电电流" prop="maxChargeCurrents">
          <el-input v-model="form.maxChargeCurrents" placeholder="请输入最高允许充电电流" />
        </el-form-item>
        <el-form-item label="最高允许充电总电压" prop="maxCountChargeVoltage">
          <el-input v-model="form.maxCountChargeVoltage" placeholder="请输入最高允许充电总电压" />
        </el-form-item>
        <el-form-item label="最高允许温度" prop="maxTemperature">
          <el-input v-model="form.maxTemperature" placeholder="请输入最高允许温度" />
        </el-form-item>
        <el-form-item label="车辆号" prop="vehicleNum">
          <el-input v-model="form.vehicleNum" placeholder="请输入车辆号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVehicle, getVehicle, delVehicle, addVehicle, updateVehicle } from "@/api/business/vehicle";
import { tablesFilter } from '@/utils/tablesFilter'

export default {
  name: "Vehicle",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆信息表格数据
      vehicleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        batteryGroupCapacity: null,
        batteryGroupChargeTimes: null,
        batteryGroupManufactureDate: null,
        batteryGroupSerial: null,
        batteryGroupVoltage: null,
        batteryManufacturer: null,
        batteryType: null,
        consumerId: null,
        countBatteryeenergy: null,
        directCharge: null,
        maxBatteryCellChargeVoltage: null,
        maxChargeCurrents: null,
        maxCountChargeVoltage: null,
        maxTemperature: null,
        vehicleNum: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      colData: [
        /*{ title: "车辆识别码", istrue: true },
        { title: "电池组额定容量", istrue: true },
        { title: "电池组充电次数", istrue: true },
        { title: "电池组生产日期", istrue: true },
        { title: "电池组序号", istrue: true },
        { title: "电池组额定电压", istrue: true },
        { title: "电池生产厂家", istrue: true },
        { title: "电池类型", istrue: true },
        { title: "所属用户", istrue: true },
        { title: "标称总能量", istrue: true },
        { title: "免扫码授权", istrue: true },
        { title: "单体电池最高允许充电电压", istrue: true },
        { title: "最高允许充电电流", istrue: true },
        { title: "最高允许充电总电压", istrue: true },
        { title: "单体电池最高允许充电电压", istrue: true },
        { title: "最高允许温度", istrue: true },
        { title: "车辆号", istrue: true },*/
      ],
      checkBoxGroup: [],
      checkedColumns: [],
      colDate: []
    };
  },
  watch: {
    checkedColumns(val, value) {
      let arr = this.checkBoxGroup.filter((i) => !val.includes(i)); // 未选中
      //localStorage.setItem(this.colTable, JSON.stringify(arr));
      this.colData.filter((i) => {
        if (arr.indexOf(i.title) != -1) {
          i.istrue = false;
        } else {
          i.istrue = true;
        }
      });
    },
  },
  created() {
    this.getList();
  },
  mounted() {
    this.$nextTick(() => {
      //console.log(this.$refs['myTable'].$children)
      const myTableData = tablesFilter('myTable', this.$refs)
      myTableData.forEach((item, index) => {
        this.checkBoxGroup.push(item.title);
        this.checkedColumns.push(item.title);
        this.colDate.push({
          label: item.title,
          prop: item.prop
        })
      });
    })
  },
  methods: {
    createData() {
      console.log(111111111)
    },
    deleteData() {

    },
    /** 查询车辆信息列表 */
    getList() {
      this.loading = true;
      listVehicle(this.queryParams).then(response => {
        this.vehicleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        vin: null,
        batteryGroupCapacity: null,
        batteryGroupChargeTimes: null,
        batteryGroupManufactureDate: null,
        batteryGroupSerial: null,
        batteryGroupVoltage: null,
        batteryManufacturer: null,
        batteryType: null,
        consumerId: null,
        countBatteryeenergy: null,
        directCharge: null,
        maxBatteryCellChargeVoltage: null,
        maxChargeCurrents: null,
        maxCountChargeVoltage: null,
        maxTemperature: null,
        vehicleNum: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.vin)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const vin = row.vin || this.ids
      getVehicle(vin).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.vin != null) {
            updateVehicle(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVehicle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const vins = row.vin || this.ids;
      this.$modal.confirm('是否确认删除车辆信息编号为"' + vins + '"的数据项？').then(function() {
        return delVehicle(vins);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/vehicle/export', {
        ...this.queryParams
      }, `vehicle_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
